#include "../Header Files/Direct3D_AL.h"

void Direct3D_AL::Init(HINSTANCE _HInstance, int _nWindowWidth, int _nWindowHeight)
{
	m_pHInstance = _HInstance;
	m_nWindowHeight = _nWindowHeight;
	m_nWindowWidth = _nWindowWidth;
}

Direct3D_AL::Direct3D_AL()
{

}

Direct3D_AL* Direct3D_AL::GetInstance()
{
	static Direct3D_AL instance;
	return &instance;
}

Direct3D_AL::~Direct3D_AL()
{
}

bool Direct3D_AL::InitializeWindow(WNDPROC _pWndProc)
{
	WNDCLASSEX	wndClass;
	ZeroMemory(&wndClass, sizeof(wndClass));
	wndClass.cbSize = sizeof(WNDCLASSEX);					// size of window structure
	wndClass.lpfnWndProc = _pWndProc;				// message callback
	wndClass.lpszClassName = "DirectXApplication";          // class name
	wndClass.hInstance = m_pHInstance;								// handle to application
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);			// default cursor
	wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME);	// background brush
	RegisterClassEx(&wndClass);

	// create window of registered type
	m_pHWND = CreateWindow(
		"DirectXApplication", "DirectX Application",    // class name, window title 
		WS_OVERLAPPEDWINDOW,							// window style
		CW_USEDEFAULT, CW_USEDEFAULT,                   // x & y coordinates
		m_nWindowWidth, m_nWindowHeight,                // width & height of window
		NULL, NULL,                                     // parent window & menu
		m_pHInstance,											// handle to application
		NULL);

	// take a guess
	ShowWindow(m_pHWND, SW_SHOW);

	return true;
}

void Direct3D_AL::InitializeDirectX(bool _bWindowed)
{
	m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.hDeviceWindow = m_pHWND;
	d3dpp.Windowed = _bWindowed;
	d3dpp.BackBufferWidth = m_nWindowWidth;
	d3dpp.BackBufferHeight = m_nWindowHeight;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferFormat = (_bWindowed) ? D3DFMT_UNKNOWN : D3DFMT_X8R8G8B8;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

	d3dpp.EnableAutoDepthStencil = true;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	HRESULT test = m_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, m_pHWND,
		D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDev);
	
	D3DXCreateSprite(m_pD3DDev, &m_pSpriteManager);
}
